package com.portal.email.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class EmailListener {
    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 监听邮件队列，发送邮件
     * 队列名称：email.queue
     */
    @RabbitListener(queues = "email.queue")  // 监听指定的队列消息
    public void sendEmail(Map<String,String> emailData) {
        log.info("监听到消息：{}", emailData);

        // 获取邮件信息
        String from = emailData.get("from");
        String to = emailData.get("to");
        String subject = emailData.get("subject");
        String content = emailData.get("content");
        // 发送邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        // 发送成功一次以后，开发阶段把这个代码注释了，不用每次都发送
        javaMailSender.send(message);
    }
}
